import java.util.Arrays;

/**
 * 头、尾、中间删除元素
 */
public class Demo02 {

  public static void main(String[] args) {
    int[] arr = {11, 22, 33, 44, 55, 66, 77};
    System.out.println(Arrays.toString(arr));//11,22,33,44,55,66,77
    arr = removeTop(arr);
    System.out.println(Arrays.toString(arr));//22,33,44,55,66,77
    arr = removeLast(arr);
    System.out.println(Arrays.toString(arr));//22,33,44,55,66
    arr = remove(arr, 2);
    System.out.println(Arrays.toString(arr));//22,33,55,66

  }

  /**
   * 删除数组的顶部元素
   *
   * @param arr
   * @return
   */
  public static int[] removeTop(int[] arr) {
    return remove(arr, 0);
  }

  /**
   * 删除数组的尾部元素
   *
   * @param arr
   * @return
   */
  public static int[] removeLast(int[] arr) {
    return remove(arr, arr.length - 1);
  }

  /**
   * 删除数组的某个位置元素
   *
   * @param arr
   * @param index
   * @return
   */
  public static int[] remove(int[] arr, int index) {
    int[] newArr = new int[arr.length - 1];
    if (index == 0) {//删除顶部
//      for (int i = 0, j = 1; i < newArr.length; i++, j++) {
//        newArr[i] = arr[j];
//      }
      System.arraycopy(arr,1,newArr,0,newArr.length);
    } else if (index == arr.length - 1) {//删除尾部
//      for(int i=0;i<newArr.length;i++){
//        newArr[i] = arr[i];
//      }
      System.arraycopy(arr,0,newArr,0,newArr.length);
    }else {//删除中间
//      int i=0;
//      for(;i<index;i++){
//        newArr[i] = arr[i];
//      }
//      for(int j=i+1;i<newArr.length;i++,j++){
//        newArr[i] = arr[j];
//      }
      System.arraycopy(arr,0,newArr,0,index);
      System.arraycopy(arr,index+1,newArr,index,newArr.length-index);
    }
    return newArr;
  }
}
